//
//  Resuable.swift
//  LiveShow
//
//  Created by dai zhong on 2017/5/10.
//  Copyright © 2017年 company.cn. All rights reserved.
//

import UIKit

protocol ReuseableProtocol{
    
   
}

extension ReuseableProtocol{

    static var identifier :String { return "\(self)" }
    static var nib :UINib? { return nil }
}

extension UITableView{

    func register<T:UITableViewCell>(cellclass:T.Type) where T:ReuseableProtocol {
        
        if let nib = T.nib {
            self.register(nib, forCellReuseIdentifier: T.identifier)
        }else{
            self.register(cellclass, forCellReuseIdentifier: T.identifier )
        }
    }
    
    func dequeueReusableCell<T:UITableViewCell>(_ cellclass:T.Type ,for indexPath: IndexPath)  ->  T where T:ReuseableProtocol {
    
        return self.dequeueReusableCell(withIdentifier: T.identifier, for: indexPath) as! T
    }
}


extension UICollectionView{

    func register<T:UICollectionViewCell>(cellclass:T.Type) where T:ReuseableProtocol {
        
        if let nib = T.nib {
            self.register(nib, forCellWithReuseIdentifier: T.identifier)
        }else{
            self.register(cellclass, forCellWithReuseIdentifier: T.identifier)
        }
    }
    
    func dequeueReusableCell<T:UITableViewCell>(_ cellclass:T.Type ,for indexPath: IndexPath) ->  T where T:ReuseableProtocol {
        
        return self.dequeueReusableCell(withReuseIdentifier: T.identifier, for: indexPath) as! T
    }
}






